// pages/serviceAlert/serviceAlert.js
const API = require('../../utils/ajax.js')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    inputShowed: false, //初始文本框不显示内容
    searchList: [],
    uid: -1,
    carList: [],
    inputVal: "",
    searchType:-1,//搜索类型 1：车辆历史温控
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.carList()
    this.setData({
      uid:wx.getStorageSync('id'),
      searchType:options.searchType
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  async carList() {
    // wx.showLoading({
    //   "mask": true,
    //   "title": "加载中..."
    // });
    const res = await API.getCarList();
    let carList = [];
    // wx.hideLoading()
    if (res.code === 0) {
      const _carList = res.data;
      carList = carList.concat(_carList)
    }
    this.setData({
      carList: carList
    })
    //console.log(carList)
  },
  toCarDetail: function (e) {

    if(this.data.searchType == 1){//车辆历史温控
      wx.navigateTo({
        url: '../historyTemControl/historyTemControl?id=' + e.currentTarget.dataset.id,
      })
    }
   
    var inputVal = this.data.inputVal
    inputVal = ""
    // console.log(this.data.inputVal)
    this.carList()
    this.setData({
      inputVal
    })

  },
  bindinput: function (e) {
    var inputVal = e.detail.value
    let carList = [];
    wx.hideLoading()
    this.setData({
      inputVal: inputVal
    })
  },
  async bindconfirm(e) {
    var inputVal = e.detail.value;
    wx.showLoading({
      "mask": true,
      "title": "加载中..."
    });
    const res = await API.getCarList();
    wx.hideLoading()
    var carList = [];
    if (res.code === 0) {
      const _carList = res.data.filter(ele => {
        if (inputVal === '') {
          wx.showToast({
            title: '不能为空',
          })
          return true;
        }
        return ele.name.includes(inputVal);
      })
      carList = carList.concat(_carList)
    }

    this.setData({
      carList: carList,
      inputVal: inputVal
    })

  }
})